local hongli = fk.CreateSkill {
  name = "#rmt__hongli",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["#rmt__hongli"] = "红利",
  [":#rmt__hongli"] = "锁定技，摸牌阶段，你发明【诸葛连弩】。当你摸牌前，将牌堆的【无中生有】、伤害锦囊牌或符合你“渐营”的可用牌置顶。",

  ["$rmt__hongli"] = "得胜已是定局，何必急功近利？",
}

hongli:addEffect(fk.BeforeDrawCard, {
  mute = true,
  priority = 0.005,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hongli.name) and target == player and data.num > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cid
    -- 找诸葛连弩
    if player.phase == Player.Draw and not table.find(player:getCardIds("he"), function (id)
      return Fk:getCardById(id).name == "crossbow"
    end) then
      cid = table.find(room.draw_pile, function (id) return Fk:getCardById(id).name == "crossbow" end)
      if not cid then
        cid = table.find(room.void, function (id) return Fk:getCardById(id).name == "crossbow" end)
        if cid then
          -- 发明AK
          --room:moveCardTo(cid, Card.DrawPile, nil, fk.ReasonPut, self.name, nil, true, player)
          table.insert(room.draw_pile, 1, cid)
          table.removeOne(room.void, cid)
          room:setCardArea(cid, Card.DrawPile, nil)
          --return false
        end
      end
      room:broadcastPlaySound("./packages/aaa_Romantic/audio/skill/rmt__hongli")
      room:notifySkillInvoked(player, hongli.name, "drawcard")
    end
    -- 找无中，或者忠胆版无中
    if not cid then
      cid = table.find(room.draw_pile, function (id) return table.contains({"reinforcement", "ex_nihilo"}, Fk:getCardById(id).name) end)
    end
    -- 如果自己没AK，再找1把
    if not cid and not table.find(player:getCardIds("he"), function (id)
      return table.contains({"crossbow", "xbow"}, Fk:getCardById(id).name)
    end) then
      cid = table.find(room.draw_pile, function (id) return table.contains({"crossbow", "xbow"}, Fk:getCardById(id).name) end)
    end
    -- 啥都有了，找点好牌
    if not cid then
      local mark = player:getMark("@rmt__jianying")
      local fitJianying = function (card)
        return mark == 0 or (card.number == mark[2] or card:getSuitString(true) == mark[1])
      end
      cid = table.find(room.draw_pile, function (id)
        local c = Fk:getCardById(id)
        return (fitJianying(c) or math.random() < 0.4) and (
        (c.name == "peach" and player:isWounded())
        or (c.type == Card.TypeTrick and not c.is_passive)
        or (c.trueName == "slash" and player:canUse(c) and math.random() < 0.4)
        )
      end)
    end
    if cid then
      table.removeOne(room.draw_pile, cid)
      table.insert(room.draw_pile, 1, cid)
      room:syncDrawPile()
    end
  end,
})

return hongli
